package SpaceIT.pages;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import model.pojo.Cart;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.util.TextStreamResponse;
public class AjaxCenter 
{
	@Inject
	private Request request;
	
	@ApplicationState
	private Cart cart;
	
	public StreamResponse onActivate(){
//	{
		String s = request.getParameter("Action");
		if(s != null)
		{
			if(s.equals("0"))
			{
				model.pojo.ItemCart item = new model.pojo.ItemCart();
				
				item.setMaSP(request.getParameter("MaSP"));
				item.setGiaBan(Integer.parseInt(request.getParameter("GiaBan")));
				item.setTenSP(request.getParameter("TenSP"));
				item.setLinkHinh(request.getParameter("HinhAnh"));
				item.setLinkChiTiet(request.getParameter("linkCT"));
				
				cart.Add(item);
				return new TextStreamResponse("text/plain;charset=UTF-8", "T");		
			}
			if(s.equals("1"))
			{
				String MaSP = request.getParameter("MaSP");
				cart.Remove(MaSP);
				return new TextStreamResponse("text/plain;charset=UTF-8", "T");
			}
			if(s.equals("2"))
			{
				String MaSP = request.getParameter("MaSP");
				int SoLuong = Integer.parseInt(request.getParameter("SoLuong"));
				cart.CapNhatSoLuong(MaSP, SoLuong);
				return new TextStreamResponse("text/plain;charset=UTF-8", "T");
			}
			if(s.equals("3"))
			{
				int tt = cart.TongTien();
				DecimalFormat myFormatter = new DecimalFormat("###,###");
				String kq = "T" + myFormatter.format(tt);
				return new TextStreamResponse("text/plain;charset=UTF-8", "T");
			}
		}
		return new TextStreamResponse("text/plain;charset=UTF-8", "");
	}
}
